<?php


namespace App\severces;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class JwtSeverce
{
    /**
     * jwt加密
     * @param array $data
     * @param int $exprice
     */
    public static function jwtEncode($data=[],$exprice = 7200)
    {
        try {
            $key = '2008A';
            $payload = [
                'data' => $data,
                'exprice'=>time() + $exprice
            ];

            $jwt = JWT::encode($payload, $key, 'HS256');

            return $jwt;
        }catch (\Exception $e){

            exit(json_encode([
                'error_code'=>666,
                'data'=>null,
                'msg'=>'jwt解析失败'
            ],FILEINFO_RAW));

        }

    }

    /**
     * jwt 解密
     * @param $jwt
     * @return array
     */
    public static function jwtDecode($jwt)
    {
        $key = '2008A';

        $decoded = JWT::decode($jwt, new Key($key, 'HS256'));

        $decoded_array = (array) $decoded;

        return $decoded_array;
    }
}
